<template>
  <div class="course">
    <div class="course-header">
      <div class="title">{{ datas.form.title }}</div>
      <div class="more"><i class="el-icon-more right"></i></div>
    </div>
    <!-- 其他课程 -->
    <template>
      <!-- 列表 -->
      <div v-if="datas.form.arrange_type == 1" class="course-body list">
        <div
          v-for="(item, index) in datas.mock_source"
          :key="index"
          class="item"
        >
          <div class="image">
            <el-image :src="item.cover_url">
              <div slot="error" class="image-error">
                <img
                  class="image-error__icon"
                  src="@/assets/images/template/h5/placeholder_medium.png"
                />
              </div>
            </el-image>

            <!-- 活动图标 -->
            <img
              v-if="datas.form.source === 'event'"
              class="activity-icon"
              src="@/assets/images/template/h5/course_discount_icon.png"
            />

            <!-- tag -->
            <img class="tag-icon" :src="tabIconImg[item.course_type]" />
          </div>
          <div class="desc">
            <div class="title">{{ item.title }}</div>
            <div class="bottom">
              <div class="number">{{ item.num }}人报名</div>
              <div class="price">¥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 宫格 -->
      <div v-else-if="datas.form.arrange_type == 2" class="course-body grid">
        <div
          v-for="(item, index) in datas.mock_source"
          :key="index"
          class="item"
        >
          <div class="image">
            <el-image :src="item.cover_url">
              <div slot="error" class="image-error">
                <img
                  class="image-error__icon"
                  src="@/assets/images/template/h5/placeholder_medium.png"
                />
              </div>
            </el-image>

            <!-- 活动图标 -->
            <img
              v-if="datas.form.source === 'event'"
              class="activity-icon"
              src="@/assets/images/template/h5/course_discount_icon.png"
            />

            <!-- tag -->
            <img class="tag-icon" :src="tabIconImg[item.course_type]" />
          </div>
          <div class="desc">
            <div class="title">{{ item.title }}</div>
            <div class="bottom">
              <div class="number">{{ item.num }}人报名</div>
              <div class="price">¥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 滚动 -->
      <div v-else-if="datas.form.arrange_type == 3" class="course-body scroll">
        <el-scrollbar>
          <div class="list">
            <div
              v-for="(item, index) in datas.mock_source"
              :key="index"
              class="item"
            >
              <div class="image">
                <el-image :src="item.cover_url">
                  <div slot="error" class="image-error">
                    <img
                      class="image-error__icon"
                      src="@/assets/images/template/h5/placeholder_medium.png"
                    />
                  </div>
                </el-image>

                <!-- 活动图标 -->
                <img
                  v-if="datas.form.source === 'event'"
                  class="activity-icon"
                  src="@/assets/images/template/h5/course_discount_icon.png"
                />

                <!-- tag -->
                <img class="tag-icon" :src="tabIconImg[item.course_type]" />
              </div>
              <div class="desc">
                <div class="title">{{ item.title }}</div>
                <div class="bottom">
                  <div class="number">{{ item.num }}人报名</div>
                  <div class="price">¥{{ item.price }}</div>
                </div>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </template>

    <!-- 删除组件 -->
    <slot name="deles" />
  </div>
</template>

<script>
export default {
  name: 'Course',
  props: {
    datas: {
      type: Object,
      default: () => {}
    },
    isDroping: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      tabIconImg: {
        1: require('@/assets/images/template/h5/dianbocourse_icon.png'),
        2: require('@/assets/images/template/h5/livecourse_icon.png'),
        3: require('@/assets/images/template/h5/facecourse_icon.png'),
        4: require('@/assets/images/template/h5/classcourse_icon.png')
      }
    }
  },
  watch: {
    'datas.form.source_index': {
      handler() {
        this.loadBatchInfo()
      }
    }
  },
  created() {
    if (this.isDroping === false) {
      this.loadBatchInfo()
    }
  },
  methods: {
    loadBatchInfo() {
      let sourceList = []
      if (this.datas.form.source === 'custom') {
        const ids = this.datas.form.source_list.map(item => item.value)
        if (ids.length > 0) {
          this.$http.get(
            '/course/admin/batch/info',
            {
              ids: ids
            },
            ({ data }) => {
              this.datas.form.source_list.forEach(item => {
                const info = data.find(i => i.id === item.value)
                sourceList.push({
                  id: info.id,
                  title: info.title,
                  course_type: info.course_type,
                  num: info.sale_count_market + info.sale_count,
                  price: info.price,
                  cover_url: info.cover_url
                })
              })
            },
            _ => {}
          )
        }
      } else {
        sourceList = this.getMockData()
      }

      this.$set(this.datas, 'mock_source', sourceList)
    },
    // 课程模拟数据
    getMockData() {
      const mockData = []
      const videoCourse = {
        title: '示例点播课程',
        course_type: 1,
        num: 123,
        price: 99.99,
        cover_url: ''
      }
      const liveCourse = {
        title: '示例直播课程',
        course_type: 2,
        num: 123,
        price: 99.99,
        cover_url: ''
      }
      const classesCourse = {
        title: '示例班级课程',
        course_type: 4,
        num: 123,
        price: 99.99,
        cover_url: ''
      }

      switch (this.datas.form.course_type) {
        case 2:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...liveCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
        case 4:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...classesCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
        default:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...videoCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
      }

      return mockData
    }
  }
}
</script>

<style scoped lang="scss">
.course {
  padding: 14px 15px;
  background: #ffffff;
  position: relative;

  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title {
      font-size: 17px;
      font-weight: 500;
      color: #303133;
      line-height: 24px;
    }

    .more {
      font-size: 12px;
      font-weight: 400;
      color: #888888;
      line-height: 16px;
    }
  }

  &-body {
    margin-top: 15px;

    .item {
      .image {
        position: relative;

        .el-image {
          width: 100%;
          height: 100%;

          ::v-deep.image-error {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 100%;
            background-color: #f1f1f1;
            border-radius: 8px;

            .image-error__icon {
              width: 40px;
              height: 28px;
            }
          }
        }

        .activity-icon {
          position: absolute;
          right: 0;
          bottom: 0;
        }

        .tag-icon {
          position: absolute;
          left: 0;
          top: 0;
        }
      }

      .title {
        font-size: 14px;
        font-weight: 500;
        color: #333333;
        line-height: 20px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }

      .number {
        font-size: 11px;
        font-weight: 400;
        color: #888888;
        line-height: 16px;
      }

      .price {
        font-size: 16px;
        font-weight: 500;
        color: #f54030;
        line-height: 22px;
      }
    }

    &.list {
      box-shadow: 0px 0px 20px 0px rgba(48, 49, 51, 0.06);
      border-radius: 10px;
      padding: 10px;

      .item {
        padding: 5px 0;
        display: flex;

        .image {
          width: 140px;
          height: 78px;
        }

        .desc {
          flex: 1;
          min-width: 0;
          margin-left: 10px;
        }

        .bottom {
          margin-top: 36px;
        }
      }
    }

    &.grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-gap: 14px 10px;

      .item {
        padding: 15px 10px;
        box-shadow: 0px 0px 20px 0px rgba(48, 49, 51, 0.06);
        border-radius: 8px;
        min-width: 0;

        .image {
          width: 100%;
          height: 86px;
        }

        .desc {
          margin-top: 8px;
        }

        .bottom {
          margin-top: 10px;
        }
      }
    }

    &.scroll {
      ::v-deep.el-scrollbar {
        width: 100%;
        padding-bottom: 15px;
      }

      ::v-deep.el-scrollbar__wrap {
        width: 100%;
        overflow-y: hidden;
        overflow-x: auto;
      }

      ::v-deep.el-scrollbar__view {
        width: 100%;
      }

      .list {
        display: flex;
      }

      .item {
        flex: none;
        width: 156px;
        padding: 0 5px;

        .image {
          width: 100%;
          height: 86px;
        }

        .desc {
          margin-top: 8px;
        }

        .bottom {
          margin-top: 10px;
        }
      }
    }
  }

  // 直播
  &-live {
    margin-top: 15px;

    .item {
      min-width: 0;
      position: relative;

      .activity-icon {
        position: absolute;
        right: 0;
        top: 0;
      }

      .image {
        position: relative;

        .el-image {
          width: 100%;
          height: 100%;
          border-radius: 100%;

          ::v-deep.image-error {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 100%;
            background-color: #f1f1f1;
            border-radius: 8px;

            .image-error__icon {
              font-size: 30px;
              color: #e0e0e0;
            }
          }
        }

        .tag {
          padding: 2px 5px;
          font-size: 12px;
          font-weight: 400;
          color: #ffffff;
          line-height: 14px;
          background: #67c23a;
          border-radius: 4px;
          position: absolute;
          bottom: -8px;
          left: 50%;
          transform: translateX(-50%);
          white-space: nowrap;

          .icon {
            width: 12px;
            margin-right: 2px;
          }
        }
      }

      .content {
        .title {
          font-size: 14px;
          font-weight: 500;
          color: #303133;
          line-height: 18px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .bottom {
          display: flex;
          align-items: center;

          .text {
            font-size: 12px;
            font-weight: 400;
            color: #909399;
            line-height: 17px;
          }

          .text + .text {
            margin-left: 15px;
          }

          .price {
            font-size: 15px;
            font-weight: 500;
            color: #f54030;
            line-height: 21px;
            margin-left: auto;
          }
        }
      }

      .time {
        font-size: 11px;
        font-weight: 400;
        color: #303133;
        line-height: 16px;
        padding: 4px 9px;
        background: #f7f7f7;
        white-space: nowrap;
      }
    }

    &.list {
      .item {
        display: flex;
        align-items: center;
        padding: 20px 15px;
        box-shadow: 0px 0px 20px 0px rgba(48, 49, 51, 0.06);
        border-radius: 8px;

        .image {
          width: 60px;
          height: 60px;
        }

        .time {
          margin-top: 8px;
          display: inline-block;
          border-radius: 14px;
        }

        .content {
          flex: 1;
          min-width: 0;
          margin-left: 12px;

          .bottom {
            margin-top: 8px;
          }
        }
      }

      .item + .item {
        margin-top: 10px;
      }
    }

    &.grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-gap: 14px 10px;

      .item {
        padding: 16px 13px;
        box-shadow: 0px 0px 20px 0px rgba(48, 49, 51, 0.06);
        border-radius: 8px;

        .time {
          border-radius: 4px;
          position: absolute;
          bottom: -5px;
          left: 50%;
          transform: translateX(-50%);
        }

        .image {
          width: 85px;
          height: 85px;
          margin: 0 auto;
        }

        .content {
          margin-top: 20px;

          .title {
            text-align: center;
          }

          .bottom {
            margin-top: 10px;
          }
        }
      }
    }

    &.scroll {
      ::v-deep.el-scrollbar {
        width: 100%;
      }

      ::v-deep.el-scrollbar__wrap {
        width: 100%;
        overflow-y: hidden;
        overflow-x: auto;
      }

      ::v-deep.el-scrollbar__view {
        width: 100%;
      }

      .ul {
        display: flex;
      }

      .item {
        flex: none;
        width: 167px;
        padding: 16px 13px;
        box-shadow: 0px 0px 20px 0px rgba(48, 49, 51, 0.06);
        border-radius: 8px;
        box-sizing: border-box;

        .time {
          border-radius: 4px;
          position: absolute;
          bottom: -5px;
          left: 50%;
          transform: translateX(-50%);
        }

        .image {
          width: 85px;
          height: 85px;
          margin: 0 auto;
        }

        .content {
          margin-top: 20px;

          .title {
            text-align: center;
          }

          .bottom {
            margin-top: 10px;
          }
        }
      }

      .item + .item {
        margin-left: 10px;
      }
    }
  }
}
</style>
